﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ProjectFinal {
	public partial class frmCheckOut : Form {
		// Declaration of external function.
		[DllImport("user32.dll")]
		private static extern int SendMessage(int hWnd, int wMsg,
				int wParam, ref int lParam);

		// Windows message constant for setting the tab stops for a list box
		private const int LB_SETTABSTOPS = 0x192;

		public frmCheckOut() {
			InitializeComponent();
			setListBoxes();
			lblLimit.Hide();
			lblLeft.Hide();
			BusinessRules.listPatrons(listBoxPatron);
		}

		//sets the tab width for listboxes
		private void setListBoxes() {
			// Set Tabs
			int[] ListBoxTabs = new int[] { 200, 240 };
			int result;

			// Send an LB_SETTABSTOPS message to the ListBox control.
			result = SendMessage(this.listBoxCurrentItems.Handle.ToInt32(),
					LB_SETTABSTOPS, ListBoxTabs.Length, ref ListBoxTabs[0]);
		}

		private void select_Patron(object sender, EventArgs e) {
			if(listBoxPatron.SelectedIndex != -1) {
				listBoxBookCart.Items.Clear();
				BusinessRules.clearCart();
				buildLists();
				checkBtnStatus();
				richTextBoxBookInfo.Text = "";
				textBox1.Clear();
			}
		}

		private void buildLists() {
			//show all media for adults
			//but only childrens books for children
			if(BusinessRules.getPatronInfo(listBoxPatron.SelectedIndex).Adult) {
				BusinessRules.listItems(listBoxAvailableItems);
			} else {
				BusinessRules.listChildrensBooks(listBoxAvailableItems);
			}

			BusinessRules.listPatronItems(listBoxCurrentItems, listBoxPatron.SelectedIndex);
			lblLimit.Show();
			lblLimit.Text = BusinessRules.patronLimit(listBoxPatron.SelectedIndex).ToString();
			lblLeft.Show();
			lblLeft.Text = BusinessRules.patronItemsLeft(listBoxPatron.SelectedIndex).ToString();
		}

		private void checkBtnStatus() {
			if(BusinessRules.patronItemsLeft(listBoxPatron.SelectedIndex) > 0)
				btnAddBookToCart.Enabled = true;
			else
				btnAddBookToCart.Enabled = false;
		}

		private void updateTotalLeftLabel() {
			//total items patron can still checkout
			int total = BusinessRules.patronItemsLeft(listBoxPatron.SelectedIndex);
			//update the items left count
			int count = total - listBoxBookCart.Items.Count;
			lblLeft.Text = count.ToString();

			//if count = 0 turn off the add to cart button
			if(count == 0)
				btnAddBookToCart.Enabled = false;
			else
				btnAddBookToCart.Enabled = true;
		}

		private void button1_Click(object sender, EventArgs e) {
			this.Close();
		}

		private void btnAddBookToCart_Click(object sender, EventArgs e) {
			if(listBoxPatron.SelectedIndex != -1 && listBoxAvailableItems.SelectedIndex != -1) {
				//total items patron can still checkout
				int total = BusinessRules.patronItemsLeft(listBoxPatron.SelectedIndex);

				//if the patron has not exceeded their checkout limit add item to cart
				if(listBoxBookCart.Items.Count < total)
					BusinessRules.addItemToCart(listBoxBookCart, listBoxAvailableItems, listBoxAvailableItems.SelectedIndex);

				//update the items left count
				updateTotalLeftLabel();
			}
		}

		private void btnRemoveBookFromCart_Click(object sender, EventArgs e) {
			if(listBoxPatron.SelectedIndex != -1 && listBoxBookCart.SelectedIndex != -1) {
				//remove from cart
				BusinessRules.removeItemFromCart(listBoxBookCart, listBoxAvailableItems, listBoxBookCart.SelectedIndex);

				//refresh available list with appropriate items
				BusinessRules.listItems(listBoxAvailableItems);

				//update the items left count
				updateTotalLeftLabel();
			}
		}

		private void btnCheckOut_Click(object sender, EventArgs e) {
			int patronIndex = listBoxPatron.SelectedIndex;

			if(patronIndex != -1) {
				if(listBoxBookCart.Items.Count > 0) {
					for(int i = 0; i < listBoxBookCart.Items.Count; ++i) {
						if(!BusinessRules.checkOut(BusinessRules.getPatronInfo(patronIndex),
								BusinessRules.getItemCartInfo(i)))
							MessageBox.Show("You can not check out any aditional items.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}

				BusinessRules.clearCart();
				listBoxBookCart.Items.Clear();
				listBoxCurrentItems.Items.Clear();
				buildLists();
			}
		}

		private void listBoxAvailableBooks_SelectedIndexChanged(object sender, EventArgs e) {
			if(listBoxAvailableItems.SelectedIndex != -1)
				BusinessRules.displayItemInfo(richTextBoxBookInfo, listBoxAvailableItems.SelectedIndex);
			else
				richTextBoxBookInfo.Text = "";

		}

		private void searchAvailable() {
			if(listBoxPatron.SelectedIndex == -1 || BusinessRules.getPatronInfo(listBoxPatron.SelectedIndex).Adult)
				BusinessRules.search(listBoxAvailableItems, textBox1.Text);
			else
				BusinessRules.childrensSearch(listBoxAvailableItems, textBox1.Text);
		}

		private void textBox1_KeyDown(object sender, KeyEventArgs e) {
			if(e.KeyCode == Keys.Enter) {
				searchAvailable();
			}
		}

		private void button1_Click_1(object sender, EventArgs e) {
			searchAvailable();
		}

		private void button2_Click(object sender, EventArgs e) {
			textBox1.Clear();
			searchAvailable();
		}
	}
}
